'use strict';

const Controller = require('egg').Controller;

class JspangController extends Controller {
  async index() {
    const { ctx } = this;
    // ctx.body = '<h1>this is a Jspang</h1>';
    // 使用模板引擎
    // await ctx.render('jspang.html')  //app下的view文件夹
    await ctx.render('jspang.html',{
      id:2021,
      name:'chanqi',
      hobby:[
        'soccer',
        'basketball',
        'pingpong'
      ]

    })
  }
  async getGirls() {
    const { ctx } = this;
    await new Promise(resolve => {
      setTimeout(() => {
        resolve(ctx.body = '<h1>this is my girl please</h1>');
      }, 5000);
    });
  }
  // 自由传参模式
  async getGirl() {
    const { ctx } = this
    const res = await ctx.service.jspang.getGirl('1')
    ctx.body = res
  }

  // 严格传参模式
  async getGirl2() {
    const { ctx } = this
    ctx.body = `我的名字是${ctx.params.name}`
  }

  async getGirl3() {
    const { ctx } = this
    ctx.body = `我的名字是${ctx.params.name}, 我${ctx.params.age}岁`
  }

  // post请求
  async add() {
    const { ctx } = this
    ctx.body = {
      status:200,
      data:ctx.request.body
    }
  }

  
}

module.exports = JspangController;

/**
 * RESTFUL Controller
 * HTML URL HTML  渲染页面
 * 代理服务器
 */
